package com.github.baichuan.application.scaffold.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 用户操作审计配置
 */
@Configuration
public class Audit {
    @Autowired
    private Environment env;

    @Bean
    @Qualifier("audit-jdbc-template")
    @ConditionalOnProperty(value = "audit.enable", havingValue = "true")
    public JdbcTemplate getJdbcTemplate() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(env.getRequiredProperty("audit.datasource.driver-class-name"));
        config.setJdbcUrl(env.getRequiredProperty("audit.datasource.url"));
        config.setUsername(env.getRequiredProperty("audit.datasource.username"));
        config.setPassword(env.getRequiredProperty("datasource.audit.password"));

        HikariDataSource dataSource = new HikariDataSource(config);

        return new JdbcTemplate(dataSource);
    }
}
